﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Infrastructures;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.基础设置;
using Yitter.IdGenerator;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.基础设置
{
    /// <summary>
    /// 添加设备管理命令处理方法
    /// </summary>
    public class AddDeviceCommandHandler:IRequestHandler<AddDeviceCommand,APIResult<int>>
    {
        /// <summary>
        /// sqlsugar
        /// </summary>
        private readonly SqlSugarContext sugar;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sugar"></param>
        public AddDeviceCommandHandler(SqlSugarContext sugar,IMapper mapper)
        {
            this.sugar = sugar;
            this.mapper = mapper;
        }
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<int>> Handle(AddDeviceCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {
                //开启事务
                sugar.db.Ado.BeginTran();

                var model = mapper.Map<Device>(request);
                model.Device_Code = YitIdHelper.NextId();
                model.Bind_Status = 1;
                model.IsDel = false;
                var list = sugar.db.Queryable<Device>().Where(x => x.Device_Name == request.Device_Name).FirstAsync();
                if(list==null)
                {
                    var r = await sugar.db.Insertable(model).ExecuteCommandAsync();
                    if (r > 0)
                    {
                        // 提交事务
                        sugar.db.Ado.CommitTran();
                        result.Code = APIEnum.添加成功;
                        result.Msg = APIEnum.添加成功.ToString();
                        result.data = r;

                    }
                    else
                    {
                        // 回滚事务
                        sugar.db.Ado.RollbackTran();
                        result.Code = APIEnum.添加失败;
                        result.Msg = APIEnum.添加失败.ToString();
                        result.data = r;
                    }
                }
                else
                {
                    result.Code = APIEnum.操作失败;
                    result.Msg = APIEnum.操作失败.ToString()+"设备名称已存在";
                }
                
            }
            catch (Exception ex)
            {
                // 发生异常时回滚事务
                sugar.db.Ado.RollbackTran();
                // 记录异常信息
                Console.WriteLine($"添加设备管理信息时发生异常: {ex.Message}");
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = 0;
            }
            return await Task.FromResult(result);
        }



    }

}
